﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace FEP
{
    public  class FepClient
    {
        const string ClientPath = "C:\\Program Files\\Microsoft Security Client\\Antimalware\\MpCmdRun.exe";
        const string FullScanCMD = " -scan -1";
        const string QuickScanCMD = " -scan -2";
        public string DoCMD(FEP.FepCom.ActionType type, string args)
        {
            string retval = "";
            try
            {
                switch (type)
                {
                    case FepCom.ActionType.FullScan:
                        ProcessRun(ClientPath, FullScanCMD);
                        break;
                    case FepCom.ActionType.QuickScan:
                        ProcessRun(ClientPath, QuickScanCMD);
                        break;
                    case FepCom.ActionType.Nothing:
                        retval = "No Action Taken";
                        break;
                    default:
                        break;


                }



            }
            catch (Exception ex) { retval ="Exception: "+ ex.ToString(); }

            return retval;
        }
         //   Nothing = 0,
         //   FullScan = 1,
         //   QuickScan = 2,
         //   CustomSingleFileScan = 3,
         //   UpdateDefinitions = 4,
         //   RemoveDefinitions = 5,
         //   RemoveAllDynamicSignatures = 6,
         //   AddDynamicSignature = 7,
         //   ListAllDynamicSignatures = 8,
         //   RemoveDynamicSignatures = 9,
         //   Trace = 10,
         //   GetFiles = 11,
         //   RedirectToNewHost = 12,
         //   UpdateFEPClientService = 13,
         //   UpdateFEPClient = 14
        public string ProcessRun(string CMD, string Args)
        {
            string retval = "";
            try
            {
                Process p = new Process();
                // Redirect the output stream of the child process.
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = CMD;
                p.StartInfo.Arguments = Args;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.Start();
                // Do not wait for the child process to exit before
                // reading to the end of its redirected stream.
                // p.WaitForExit();
                // Read the output stream first and then wait.
                retval = p.StandardOutput.ReadToEnd();
                p.WaitForExit();

            }
            catch (Exception ex)
            {
                retval = ex.ToString();
            }

            return retval;

        }
    }
}
